package com.jianjun.project.system.menu;

import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
import com.jianjun.common.constant.UserConstants;
import com.jianjun.common.util.SecurityUtils;
import com.jianjun.framework.web.domain.AjaxResult;
import com.jianjun.framework.web.interceptor.DataScope;
import com.jianjun.project.common.model.SysMenu;

import java.util.List;

/**
 * 菜单信息
 *
 * @author source
 */
@Path(value = "/system/menu")
public class SysMenuController extends Controller {
    @Inject
    SysMenuService menuService;

    /**
     * 获取菜单列表
     */
    @DataScope(permission = "system:menu:list")
    public void list() {
        SysMenu menu = JsonKit.parse(getKv().toJson(), SysMenu.class);
        List<SysMenu> menus = menuService.selectMenuList(menu, SecurityUtils.getUserId());
        renderJson(AjaxResult.success(menus));
    }

    @DataScope(permission = "system:menu:query,system:menu:add,system:menu:edit,system:menu:remove")
    public void index() {
        String method = getRequest().getMethod();
        //查询
        if (Method.GET.name().equals(method)) {
            Long menuId = getParaToLong();
            renderJson(AjaxResult.success(menuService.selectMenuById(menuId)));
        }
        //新增
        if (Method.POST.name().equals(method)) {
            SysMenu menu = JsonKit.parse(getRawData(), SysMenu.class);
            if (!menuService.checkMenuNameUnique(menu)) {
                renderJson(AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在"));
                return;
            } else if (UserConstants.YES_FRAME.equals(String.valueOf(menu.getIsFrame())) && !HttpUtil.isHttp(menu.getPath())) {
                renderJson(AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头"));
                return;
            }
            menu.setCreateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(menuService.insertMenu(menu)));
        }
        //修改
        if (Method.PUT.name().equals(method)) {
            SysMenu menu = JsonKit.parse(getRawData(), SysMenu.class);
            if (!menuService.checkMenuNameUnique(menu)) {
                renderJson(AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在"));
                return;
            } else if (UserConstants.YES_FRAME.equals(String.valueOf(menu.getIsFrame())) && !HttpUtil.isHttp(menu.getPath())) {
                renderJson(AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头"));
                return;
            } else if (menu.getMenuId().equals(menu.getParentId())) {
                renderJson(AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己"));
                return;
            }
            menu.setUpdateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(menuService.updateMenu(menu)));
        }
        //删除
        if (Method.DELETE.name().equals(method)) {
            Long menuId = getParaToLong();
            if (menuService.hasChildByMenuId(menuId)) {
                renderJson(AjaxResult.warn("存在子菜单,不允许删除"));
                return;
            }
            if (menuService.checkMenuExistRole(menuId)) {
                renderJson(AjaxResult.warn("菜单已分配,不允许删除"));
                return;
            }
            menuService.deleteMenuById(menuId);
            renderJson(AjaxResult.success());
        }
    }


    /**
     * 获取菜单下拉树列表
     */
    public void treeselect() {
        SysMenu menu = JsonKit.parse(getKv().toJson(), SysMenu.class);
        List<SysMenu> menus = menuService.selectMenuList(menu, SecurityUtils.getUserId());
        renderJson(AjaxResult.success(menuService.buildMenuTreeSelect(menus)));
    }

    /**
     * 加载对应角色菜单列表树
     */
    public void roleMenuTreeselect() {
        Long roleId = getParaToLong();
        List<SysMenu> menus = menuService.selectMenuList(new SysMenu(), SecurityUtils.getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        renderJson(ajax);
    }
}